<?
session_start();
include_once("includes/common_public_inc.php");
$cur_grade = 0;
$id=getParamInt("id","");
	
$grade=getParamInt("grade","");
if (isset($_POST["submit"]))
	$submit = $_POST["submit"];

$tplRate=new TplLoad;
$tplRate->assign("phpself",$_SERVER['PHP_SELF']);

$rate_ad_page = 1;
if ($set_rate_ad_require_member==1)
	check_valid_user("popup");

$sql = "select ad_votes,ad_voters,ad_last_vote_ip,ad_owner from $ads_tbl where ad_id = $id";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
$ad_votes = $row["ad_votes"];
$ad_voters = $row["ad_voters"];
$ad_owner = $row["ad_owner"];
$ad_last_vote_ip = $row["ad_last_vote_ip"];

if ($ad_voters and $ad_votes)
	$cur_grade = round($ad_votes / $ad_voters, 1);

if (isset($submit) AND $grade)
{
	$ad_last_vote_ip_current = $_SERVER["REMOTE_ADDR"]; 
	
	if ($ad_last_vote_ip_current == $ad_last_vote_ip)
		$tplRate->assign("rate_fail",1);
	else if ($ad_owner==$_SESSION["valid_user"])
		$tplRate->assign("rate_fail",2);
	else 
	{
		$ad_votes = $ad_votes + $grade;
		
		if (!$ad_voters)
		{
			$sql = "update $ads_tbl set ad_voters = 0,ad_totalscore=0 where ad_id = $id";	
			$r = mysql_query($sql);
		}
		$new_grade = round($ad_votes / ($ad_voters+1), 1);
		$sql = "update $ads_tbl set ad_voters = ad_voters + 1, ad_votes = $ad_votes, ad_last_vote_ip='$ad_last_vote_ip_current',ad_totalscore=$new_grade where ad_id = $id";
		$res = q($sql);
		addToHistory(8,"",$id,"");

		$tplRate->assign("rate_ok",1);
	}

	
}
else
{

	$sql = "select ad_title from $ads_tbl where ad_id = $id";
	$res = q($sql);
	$row = mysql_fetch_array($res);
	$tplRate->assign("ad_rate_info",formatString(LA_RATE_PERSON,array($row["ad_title"]))); 
	$tplRate->assign("cur_grade",$cur_grade);	
	$tplRate->assign("ad_voters",$ad_voters);	
	$tplRate->assign("id",$id);	
	
}
$tplRate->assign("charset",$set_charset);
$tplRate->display("rate_ad.tpl");
?>

         

